/**
 * app安卓上架审核时用
 **/
// #ifdef APP-PLUS | APP-PLUS-NVUE
const Platform = 'App';
// #endif

const PermissionInfo = {
  camera: {
    permissionID: "CAMERA",
    title: "摄像头权限说明",
    content:
      "获取拍照权限用于用户对拍照图片时上传照片，请您确认授权，否则无法使用该功能",
  },
  picture: {
    permissionID: "READ_EXTERNAL_STORAGE",
    title: "读取相册权限说明",
    content:
      "获取相册权限用于用户对个人图片时上传照片，请您确认授权，否则无法使用该功能",
  },
  coarseLocation: {
    permissionID: "ACCESS_COARSE_LOCATION",
    title: "精准定位权限申请说明",
    content: "获取定位信息以推荐附近用户，请您确认授权，否则无法使用该功能",
  },
  findLocation: {
    permissionID: "ACCESS_FINE_LOCATION",
    title: "模糊定位权限申请说明",
    content: "获取定位信息以推荐附近用户，请您确认授权，否则无法使用该功能",
  },
};

// 弹窗
function modal(json) {
  uni.showModal({
    title: json.title,
    content: json.content,
    cancelText: "取消",
    confirmText: "确定",
    showCancel: true,
    confirmColor: "#000",
    cancelColor: "#666",
    success: (res) => {
      if (res.confirm) { json.cb(true); }
      if (res.cancel) { json.cb(false); }
    },
    fail(e) { json.cb(false); }
  });
}

//跳转手机系统设置
export function goSetting() {
  if (plus.os.name == "iOS") {
    var UIApplication = plus.ios.import("UIApplication");
    var application2 = UIApplication.sharedApplication();
    var NSURL2 = plus.ios.import("NSURL");
    var setting2 = NSURL2.URLWithString("app-settings:");
    application2.openURL(setting2);
    plus.ios.deleteObject(setting2);
    plus.ios.deleteObject(NSURL2);
    plus.ios.deleteObject(application2);
  } else {
    var Intent = plus.android.importClass("android.content.Intent");
    var Settings = plus.android.importClass("android.provider.Settings");
    var Uri = plus.android.importClass("android.net.Uri");
    var mainActivity = plus.android.runtimeMainActivity();
    var intent = new Intent();
    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
    intent.setData(uri);
    mainActivity.startActivity(intent);
  }
}

// 判断是否授权
export function requestPermissions(type) {
  let choosePerm = PermissionInfo[type];
  let permissionID = choosePerm.permissionID;
  return new Promise((rev, jec) => {
    if (Platform !== "App") {
      rev({ isSuc: true });
    } else {
      if (!permissionID) {
        rev({ isSuc: false });
        return false;
      }
      //判断安卓与ios设备
      if (plus.os.name == "Android") {
        let _permissionID = "android.permission." + permissionID;
        plus.android.checkPermission(
          _permissionID,
          (granted) => {
            // uni.showModal({
            //     title: "test1",
            //     content: JSON.stringify(granted),
            //     confirmText: "确定",
            //     showCancel: false,
            // });
            if (granted.checkResult == -1) { //还未授权当前查询的权限，打开权限申请目的自定义弹框
              modal({
                title: choosePerm.title,
                content: choosePerm.content,
                cb: (isSure) => {
                  if (isSure) {
                    plus.android.requestPermissions([_permissionID], (e) => {
                      // uni.showModal({
                      //     title: "test2",
                      //     content: JSON.stringify(e),
                      //     confirmText: "确定",
                      //     showCancel: false,
                      // });
                      // 当前查询权限已授权，此时可以通知页面执行接下来的操作
                      if (e.granted.length > 0) { rev({ isSuc: true }); }

                      // 本次已拒绝的权限
                      if (e.deniedPresent.length > 0) {
                        uni.showModal({
                          title: "温馨提示",
                          content: "本次已拒绝的权限",
                          confirmText: "确定",
                          showCancel: false,
                          confirmColor: "#000",
                          complete() {
                            rev({ isSuc: false });
                          },
                        });
                        rev({ isSuc: false });
                      }

                      // 永久拒绝的权限
                      if (e.deniedAlways.length > 0) {
                        uni.showModal({
                          title: "温馨提示",
                          content: "还没有该权限，立即去设置开启？",
                          cancelText: "取消",
                          confirmText: "去设置",
                          showCancel: true,
                          confirmColor: "#000",
                          cancelColor: "#666",
                          success: (res) => {
                            if (res.confirm) { goSetting(); }
                            if (res.cancel) { rev({ isSuc: false }); }
                          },
                          fail(e) {
                            rev({ isSuc: false });
                          },
                        });
                      }
                    });
                  } else { }
                }
              })
            } else {
              rev({ isSuc: true });
            }
          },
          (error) => {
            rev({ isSuc: false });
          }
        );
      } else {
        //IOS不需要添加自定义弹框来描述权限目的，因为在配置文件的隐私信息访问的许可描述里可添加
        rev({ isSuc: true });
      }
    }
  });
}
